<template>
  <div class="createPost-container">
    <el-form class="form-container" :model="postForm" :rules="rules" ref="postForm">

      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">
            <div class="postInfo-container">
              <el-row>
                  <el-tooltip  class="item" effect="dark" content="请输入标题" placement="top">
                    <el-form-item label-width="110px" label="标题：" class="postInfo-container-item" prop="title">
                      <el-input style="min-width:950px" placeholder="请输入标题" maxlength="100" v-model="postForm.title">
                        <template slot="append">您还可以输入{{ words - postForm.title.length }}个字</template>
                      </el-input>
                    </el-form-item>
                  </el-tooltip>
              </el-row>
              
              <el-row>
                  <el-form-item label="是否为热门推荐：">
                    <el-radio-group v-model="postForm.recommend">
                      <el-radio :label="1">是</el-radio>
                      <el-radio :label="0">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
              </el-row>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="上传封面：">
              <el-upload
                style="margin-left:10px"
                :headers="headers"
                action="/tools/oss/upload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-success="handleUploadSuccess"
                accept=".png,.jpg,.jpeg,.gif"
                list-type="picture"
                :file-list="fileList"
                :limit="2">
                <el-button size="small" type="primary">上传封面</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      
        <el-row>
          <el-col :span="18">
            <div class="editor-container">
              <el-tooltip  class="item" effect="dark" content="请输入内容" placement="top">
                <el-form-item label-width="110px" label="内容：" class="postInfo-container-item" prop="content">
                <Tinymce :height=400 ref="editor" v-model="postForm.content"  :value="postForm.content"/>
                </el-form-item>
              </el-tooltip>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
              <el-form-item label-width="110px" label="设置权重：" class="postInfo-container-item" prop="nickname">
                <el-input-number  v-model="postForm.weight" :step="1"></el-input-number>
              </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
              <el-form-item label-width="110px" label="设置浏览量：" class="postInfo-container-item" prop="nickname">
                <el-input-number  v-model="postForm.view" :step="1"></el-input-number>
              </el-form-item>
          </el-col>
        </el-row>

      </div>

      <div class="publish">
        <el-button v-loading="loading" type="success" :disabled="isDisable" @click="submitForm()">发布</el-button>
      </div>

      <div class="viewShow">
        <el-dialog :show-close="false" :visible.sync="showDialog" >
          <div class="information-cont">
            <h3>{{ postForm.title }}</h3>
            <div class="smallLine">
              <span class="time">{{ nowDate|dateformat('YYYY-MM-DD HH:mm') }}</span>
            </div>
            <div class="artical-cont" v-html="postForm.content"></div>
          </div>    
        </el-dialog>
      </div>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/headImage'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import Multiselect from 'vue-multiselect' // 使用的一个多选框组件，element-ui的select不能满足所有需求
import 'vue-multiselect/dist/vue-multiselect.min.css' // 多选框组件css
import { validateURL } from '@/utils/validate'
import { getAdd } from '@/api/research'
import { getAreaList } from '@/api/activity'
import { getToken } from '@/utils/auth' // 验权

export default {
  name: 'add',
  components: { Tinymce, MDinput, Upload, Sticky, Multiselect },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: this.i18n[rule.field] + '为必填项',
          type: 'error',
          duration: 1000
        })
        callback(new Error(this.i18n[rule.field] + '为必填项'))
      } else {
        callback()
      }
    }
    const validateArrRequire = (rule, value, callback) => {
      if (value.length === 0) {
        this.$message({
          message: this.i18n[rule.field] + '为必填项',
          type: 'error'
        })
        callback(new Error(this.i18n[rule.field] + '为必填项'))
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
        if (validateURL(value)) {
          callback()
        } else {
          this.$message({
            message: 'url填写不正确',
            type: 'error'
          })
          callback(new Error(this.i18n[rule.field] + '为必填项'))
        }
      } else {
        this.$message({
          message: this.i18n[rule.field] + '为必填项',
          type: 'error'
        })
        callback(new Error(this.i18n[rule.field] + '为必填项'))
      }
    }
    return {
      headers:{},
      fileList:[],
      tagValue:'',
      words:100,
      postForm: {
        title:'',
        content:'',
        cover:'',
        recommend: 0,
        view:10,
        weight:100
      },
      loading: false,
      i18n: {
        title: '标题'
      },
      rules: {
        title: [{ validator: validateRequire }]
      },
      isDisable: false,
      showDialog: false,
      nowDate: '', // 当前时间
      count:0

    }
  },
  mounted() {
    this.headers = { accessToken:getToken() }
  },
  methods: {

    handleUploadSuccess(rst, file, fileList){
      const { code, data:{ picUrlList } } = rst
      const { postForm } = this
      if(code == 0){
        postForm['cover'] = picUrlList[0]
        this.postForm = postForm
        this.fileList = [{ name:file.url, url:file.url }]
      }
    },
    handlePreview(file) {
    },
    handleRemove(file, fileList) {
      this.postForm['cover'] = ''
    },

    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          const params = this.postForm
          const { content, cover } = params

          if(content.trim().length == 0 || content == '<p><br data-mce-bogus="1"></p>' || content == '<p>&nbsp;</p>' || content == '<p><br></p>'){
            this.$message({
              message: '内容不能为空',
              type: 'error',
              duration: 1000
            })
            this.loading = false
            return
          }
          if(cover.length == 0){
            this.$message({
              message: '请上传封面',
              type: 'error',
              duration: 1000
            })
            this.loading = false
            return
          }
          this.isDisable = true
          getAdd(params).then(response => {
            const { code } = response
            if (code == 0) {
              this.$notify({
                title: '成功',
                message: '发布成功',
                type: 'success',
                duration: 1000
              })
              this.$router.push({ path: '/research/list' })
            }else{
              this.$message({
                title: '失败',
                message: message,
                type: 'error',
                duration: 2000
              })
            }
          }).catch(err => {})
            .finally(() => this.isDisable = false)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import 'src/styles/mixin.scss';

.viewShow {

  /deep/ .el-dialog {
    width: 350px;
    height: 760px;
    padding: 100px 12px;
    border-radius: 50px;
    background: url('../../assets/phone.png') no-repeat;
    background-size: 100% 100%; 

    .el-dialog__header {
      padding: 0;
    }

    .el-dialog__body {
      padding: 0;
      background: #fff;
      width: 100%;
      height: 100%;

      .information-cont{
        padding: 0 15px;
        background: #fff;
        white-space:normal; 
        word-break:break-all;
        height: 100%;
        overflow-y: auto;

        h3 {
          font-size: 20px;
          line-height: 32px;
          color: #333333;
          font-weight: bold;
          padding-top: 15px;
          margin: 0;

        }

        .smallLine{
          display: flex;
          display: -webkit-flex;
          justify-content:space-between;
          -webkit-justify-content: space-between;
          align-items: center;
          -webkit-align-items: center;
          font-size: 13px;
          color: #BBB;
          padding-top: 20px;
          padding-bottom: 17px;
          border-bottom: 1px solid #E5E8ED;
        }

        .artical-cont{
          padding-top: 16px;
          font-size: 15px;
          overflow: hidden;
          img{
            width: 100%;
          }
        }

      }

    }

  }

}

.upload-img-dev{
  width:320px;
  height:160px;
  position: absolute;
  right: 200px;
  bottom: 60px;
  z-index: 3;
}
.upload-img{
  width:320px;
  height:160px;
}
.tagLeft{
  margin-left:10px;
}
.title-prompt {
  position: absolute;
  right: 0px;
  font-size: 12px;
  top: 10px;
  color: #ff4949;
}
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 300px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}

.publish {
  text-align:center;
  margin: 0 auto;
  margin-bottom: 40px;
}
</style>
